<div style="margin: 15px;">
	<div class="layui-row">
		<div class="layui-col-md10">
			<form class="layui-form">
				<input type="hidden" name="templateCourseId" id="templateCourseId" value="">
				<div class="layui-form-item">
					<label class="layui-form-label">来源</label>
					<div class="layui-input-inline" style="width:auto;">
						<button class="layui-btn layui-btn-sm" type="button" style="margin-top: 4px;" id="courseConnect">从公共课程库添加</button>
					</div>
					<span class="public-prompt">若不需要公共课程提纲,请忽略该项</span>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">标题</label>
					<input type="hidden" id="createBy" name="createBy"  class="layui-input">
					<div class="layui-input-inline">
						<input type="text" name="title" placeholder="请输入" autocomplete="off" class="layui-input" lay-verify="required">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">分类</label>
					<!--<div class="layui-input-inline">-->
					<!--<input type="text" name="loginName" placeholder="请输入" autocomplete="off" class="layui-input">-->
					<!--</div>-->
					<div class="layui-input-inline">
						<!--<input type="hidden" id="menu" name="classify" placeholder="请选择" autocomplete="off" class="layui-input">-->
						<select name="classify" lay-filter="fenlei" id="dict" lay-verify="required">
							<option value="">--请选择--</option>
						</select>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">课时</label>
					<div class="layui-input-inline">
						<input type="number" name="classtime" placeholder="请输入" autocomplete="off" class="layui-input" lay-verify="required">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">学分</label>
					<div class="layui-input-inline">
						<input type="number" name="score" required lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
					</div>
					<div class="layui-form-mid layui-word-aux"> </div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">简介</label>
					<div class="layui-input-block">
						<textarea type="text" name="desc" placeholder="请输入" autocomplete="off" class="layui-textarea"></textarea>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">概览</label>
					<div class="layui-input-block">
						<!--<textarea type="text" name="overview" placeholder="请输入" autocomplete="off" class="layui-input"></textarea>-->
						<div id="editor" name="overview"></div>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">课程发布</label>
					<div class="layui-input-inline">
						<select name="delFlag" lay-filter="delFlag" id="delFlag" lay-verify="required">
							<option value="">--请选择--</option>
							<option value="2">暂不发布</option>
							<option value="0">申请发布</option>
						</select>
						<!--<input type="hidden" id="menu" name="delFlag" placeholder="请选择" autocomplete="off" class="layui-input">-->
						<!--<input type="checkbox" id="kcfb" name="" lay-skin="switch" lay-text="开启|关闭" lay-filter="fabu" checked>-->
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">课程公开</label>
					<div class="layui-input-inline" style="width: auto">
						<!--<input type="hidden" id="menu" name="delFlag" placeholder="请选择" autocomplete="off" class="layui-input">-->
						<input type="checkbox" id="hasPublic" name="hasPublic" lay-skin="switch" lay-text="是|否" lay-filter="hasPublic">
					</div>
					<span class="public-prompt">若公开,课程将进入公开课程库,任何教师用户均有权限使用</span>
				</div>
				<button lay-filter="edit" lay-submit style="display: none;"></button>
				<div class="layui-form-item">
					<label class="layui-form-label">课程图片</label>
					<div class="layui-input-inline">
						<input type="text" placeholder="图片url" name="pic" class="layui-input" id="picPath">
					</div>
					<span class="public-prompt">若图片为线上图片,可直接填入图片链接,若为本地图片,请点击"本地上传"</span>
				</div>
				<div class="layui-input-block">
					<div class="layui-upload">
						<button type="button" class="layui-btn layui-btn-sm" id="upload">本地上传</button><span id="demoText" style="color: #b66; margin-left:20px;"></span>
						<div class="layui-upload-list">
							<img class="layui-upload-img" id="demo1">
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>

<script src="../js/jquery-1.10.0.js"></script>
<script src="../js/template-native.js"></script>
<script type="text/javascript" src="js/wangEditor.min.js"></script>
<script type="text/javascript">
	var E = window.wangEditor;
	var editor = new E('#editor');
	// 或者 var editor = new E( document.getElementById('#editor') )
	editor.customConfig.uploadImgServer = '/servlet/richtextImgUploadServlet?token='+JSON.parse(getCookie('user')).token;
	editor.create();
	layui.use(['paging', 'form', 'busajax', 'upload'], function () {
		var $ = layui.jquery,
				bajax = layui.busajax,
				paging = layui.paging(),
				layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
				layer = layui.layer, //获取当前窗口的layer对象
				form = layui.form(),
				upload = layui.upload,
				user = JSON.parse(getCookie('user'));


		$("#createBy").val(user.id);

		if(user.roleType == "2"){
			$("#delFlag option").last().text("发布").attr("value","1");
		}

		var data={
			dict:{
				type:'course_tag'
			}
		};
		var url=HOST+"system.dict.get.hsr";
		$.ajax({
			url: url,
			type: 'POST',
			headers: {
				token: user.token
			},
			data: JSON.stringify(data),
			dataType: "json",
			success: function(result){
				for (var i= 0;i < result.value.length;i++){
					$("#dict").append("<option value='"+result.value[i].value+"'>"+result.value[i].label+"</option>");
				}
				form.render('select');
			},
			error:function(xhr){
				alert("错误提示： " + xhr.status + " " + xhr.statusText);
			}
		});

		var libIndex = -1;
		$("#courseConnect").on('click',function () {
			if (libIndex !== -1) {
				return;
			}
			$.get('form/courseFromLib.html', null, function (form) {
				libIndex = layer.open({
					type: 1,
					title: '公共课程库',
					content: form,
					btn: ['保存', '取消'],
					shade: false,
					offset: ['50px', '8%'],
					area: ['1000px', '500px'],
					maxmin: true,
					yes: function (index) {
						//触发表单的提交事件
//						$('form.layui-form').find('button[lay-filter=edit]').click();
						var courseId;
						$('input[name=courseId]').each(function (e) {
							if($(this).is(':checked')){
								courseId = $(this).val();
								return false;
							}
						});
						if(courseId){
							//加载课程信息
							var buscourse = {
								buscourse:{
									id: courseId
								}
							};
							$.ajax({
								url: HOST+"business.busCourse.getCourseById.hsr",
								type: 'POST',
								headers: {
									token: JSON.parse(getCookie('user')).token
								},
								data: JSON.stringify(buscourse),
								dataType: "json",
								success: function(result){
									if(result.code == 0 && result.value){
										$("#templateCourseId").val(courseId);
										var courseVal = result.value;
										$("input[name=title]").val(courseVal.title);
										$('select[name=classify]').val(courseVal.classify);
										$('input[name=classtime]').val(courseVal.classtime);
										$('input[name=score]').val(courseVal.score);
										$('textarea[name=desc]').val(courseVal.desc);
										editor.txt.html(courseVal.overview);
										$('input[name=pic]').val(courseVal.pic);
										libIndex = -1;
										layer.close(index);
										layui.form().render('select');
									}
								},
								error:function(xhr){
									alert("错误提示： " + xhr.status + " " + xhr.statusText);
								}
							});
						}

					},
					full: function (elem) {
						var win = window.top === window.self ? window : parent.window;
						$(win).on('resize', function () {
							var $this = $(this);
							elem.width($this.width()).height($this.height()).css({
								top: 0,
								left: 0
							});
							elem.children('div.layui-layer-content').height($this.height() - 95);
						});
					},
					success: function (layero, index) {
						//弹出窗口成功后渲染表单

					},
					end: function () {
						libIndex = -1;
					}
				});
			});
		});

		//普通图片上传
		var uploadInst = upload.render({
			elem: '#upload'
			,url: '/servlet/fileUploadServlet?token='+JSON.parse(getCookie('user')).token
			,before: function(obj){
				//预读本地文件示例，不支持ie8
				obj.preview(function(index, file, result){
					$('#demo1').attr('src', result); //图片链接（base64）
				});
				layer.load(); //上传loading
			}
			,done: function(res){
				var demoText = $('#demoText');
				if(res.code === 0){
					demoText.html('<span style="color: #b66;">上传成功</span>');
					$('#picPath').val(res.value[0].url);
				}
				layer.closeAll('loading'); //关闭loading
			}
			,error: function(){
				//演示失败状态，并实现重传
				var demoText = $('#demoText');
				demoText.html('<span style="color: #b66;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
				demoText.find('.demo-reload').on('click', function(){
					uploadInst.upload();
				});
				layer.closeAll('loading'); //关闭loading
			}
		});
	});
</script>
<!--<script src="../js/Global.js"></script>-->

<!-- 注意， 只需要引用 JS，无需引用任何 CSS ！！！-->